package com.dyoud.api.shop.service;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dyoud.api.shop.employee.dto.AppEmployeeInfo;
import com.dyoud.api.shop.employee.dto.EmployeeDto;
import com.dyoud.api.shop.employee.dto.EmployeePageQueryDto;
import com.dyoud.core.DozerUtils;
import com.dyoud.partner.api.model.Partner;
import com.dyoud.partner.api.service.PartnerService;
import com.dyoud.shop.api.model.ShopInfo;
import com.dyoud.shop.api.query.EmployeePage;
import com.dyoud.shop.api.query.EmployeePageQuery;
import com.dyoud.shop.api.request.EmployeeRequest;
import com.dyoud.shop.api.service.EmployeeService;
import com.dyoud.shop.api.service.ShopInfoService;
import com.dyoud.user.api.service.UserIncomeService;
import com.github.pagehelper.PageInfo;

/***
 * 
 * @author wangshuaifei
 * @date 2018/03/03
 */
@Service
public class EmployeeApiService {

    @Reference
    private EmployeeService employeeService;

    @Reference
    private UserIncomeService userIncomeService;

    @Reference
    private PartnerService partnerService;
    
    @Reference
    private ShopInfoService shopInfoService;
    /***
     * 添加员工
     * 
     * @param request
     * @return
     */
    public boolean addEmployee(EmployeeDto dto) {
        EmployeeRequest request = new EmployeeRequest();
        DozerUtils.copy(dto, request);
        return employeeService.addEmployee(request) > 0;
    }

    /***
     * 删除员工，软删除，修改状态isdel为1
     * 
     * @param employeeId
     * @return
     */
    public boolean delEmployeeByEmployeeId(Long employeeId) {
        return employeeService.delEmployeeByEmployeeId(employeeId) > 0;
    }

    /***
     * 修改员工信息
     * 
     * @param request
     * @return
     */
    public boolean updateEmployee(EmployeeDto dto) {
        EmployeeRequest request = new EmployeeRequest();
        DozerUtils.copy(dto, request);
        return employeeService.updateEmployee(request) > 0;
    }

    /***
     * 单个获取员工信息
     * 
     * @param employeeId
     * @return
     */
    public AppEmployeeInfo getEmployeeByEmployeeId(Long employeeId) {
        EmployeeRequest request = employeeService.getEmployeeByEmployeeId(employeeId);
        if (request == null) {
            return null;
        }
        AppEmployeeInfo dto = new AppEmployeeInfo();
        DozerUtils.copy(request, dto);
        return dto;
    }

    /**
     * 分页查询员工，根据店铺id,员工名称,员工是否被禁用的状态
     * 
     * @param query
     * @return
     */
    public PageInfo<EmployeePage> selectByShopidAndNameAndState(EmployeePageQueryDto query) {
        EmployeePageQuery request = new EmployeePageQuery();
        DozerUtils.copy(query, request);
        return employeeService.selectByShopidAndNameAndState(request);
    }

    /***
     * 根据用户手机和密码查询用户信息
     * 
     * @param phone
     * @param password
     * @return
     */
    public AppEmployeeInfo getEmployeeByPhoneAndPasswordAndEmail(String email, String phone, String password) {
        Partner partner = partnerService.getByEmail(email);
        ShopInfo shopInfo = shopInfoService.getByPartnerId(partner.getPartnerId());
        EmployeeRequest request = employeeService.getEmployeeByPhoneAndPasswordAndShopId(shopInfo.getShopId(), phone, password);
        if (request == null) {
            return null;
        }
        AppEmployeeInfo dto = new AppEmployeeInfo();
        DozerUtils.copy(request, dto);
        dto.setPartnerId(partner.getPartnerId());
        return dto;
    }

    public int countEmployeeCount(Long employeeId, Long shopId) {
        return userIncomeService.countEmployeeCount(employeeId, shopId);
    }

    /**
     * 操作员给予受益人人数
     * 
     * @param employeeId
     * @param shopId
     * @return
     */
    public int countEmployeePerson(Long employeeId, Long shopId) {
        return userIncomeService.countEmployeePerson(employeeId, shopId);
    };
}
